package net.xiduth.game.player.dialogues;

import java.text.DecimalFormat;

import net.xiduth.game.Animation;
import net.xiduth.game.ForceMovement;
import net.xiduth.game.ForceTalk;
import net.xiduth.game.Graphics;
import net.xiduth.game.World;
import net.xiduth.game.WorldTile;
import net.xiduth.game.player.Skills;
import net.xiduth.game.tasks.WorldTask;
import net.xiduth.game.tasks.WorldTasksManager;
import net.xiduth.utils.Utils;

/**
 * 
 * @author Plato
 * 
 */

public class MuseumGuard2 extends Dialogue {

	public static final int[] randomAnimal = { 6935, 3283, 4344, 6966 };

	/**
	 * This dialogue is sent upon the player clicking on object id: 2562.
	 */

	@Override
	public void start() {
		sendNPCDialogue(
				4405,
				9827,
				"Hey! Get your hands off that! Don't you know not to touch items in a bar!.");

	}

	@Override
	public void run(int interfaceId, int componentId) {
		switch (stage) {
		case -1:
			sendPlayerDialogue(9827, "Sorry, I just wondered what it was.");
			stage = 1;
			break;
		case 1:
			sendNPCDialogue(4405, 9827,
					"A good question.");
			stage = 2;
			break;
		case 2:
			sendNPCDialogue(
					4405,
					9827,
					"It's a cape stand that belonged to a very infamous cape.");
			stage = 3;
			break;
		case 3:
			sendPlayerDialogue(9827, "Oh, so the cape I have?");
			stage = 4;
			break;
		case 4:
			sendNPCDialogue(
					4405,
					9827,
					"Oh why yes, its just like the cape you have but a little different!");
			stage = 5;
			break;
		case 5:
			sendPlayerDialogue(9827, "What do you mean by that?");
			stage = 6;
			break;
		case 6:
			sendNPCDialogue(
					4405,
					9827,
					"The cape is enchanted with a powerful orange glowing ability.");
			stage = 7;
			break;
		case 7:
			sendPlayerDialogue(9827, "This cape is enchanted?");
			stage = 8;
			break;
		case 8:
			sendNPCDialogue(
					4405,
					9827,
					"Yes, and more than we can grasp. It physically repels anyone who tries to touch it. We had quite a hassle getting it up here.");
			stage = 9;
			break;
		case 9:
			sendPlayerDialogue(9827, "So, nobody has worn this cape?");
			stage = 10;
			break;
		case 10:
			sendNPCDialogue(
					4405,
					9827,
					"I haven't seen a person yet!");
			stage = 11;
			break;
		case 11:
			sendOptionsDialogue(SEND_DEFAULT_OPTIONS_TITLE, "Can I try?",
					"How interesting.");
			stage = 12;
			break;
		case 12:
			switch (componentId) {
			case OPTION_1:
				stage = 13;
				sendPlayerDialogue(9827, "Can I try?");
				break;
			case OPTION_2:
			default:
				stage = 30;
				sendPlayerDialogue(9827, "How interesting.");
				break;
			}
			break;

		case 13:
			sendNPCDialogue(
					4405,
					9827,
					"Sure! I have this cape around here somewhere... Ah here it is! Go ahead and try!");
			stage = 14;
			break;
		case 14:
			if (!isWorthy()) {
				player.getInterfaceManager().closeChatBoxInterface();
				player.lock();
				// final int[] randomAnimal = { 6935, 3283, 4344, 6966 };
				// final NPC guard = 4405;
				WorldTasksManager.schedule(new WorldTask() {
					int phase = 0;

					@Override
					public void run() {
						switch (phase) {
						case 0:
							player.setNextAnimation(new Animation(857));
							break;
						case 1:
							player.setNextAnimation(new Animation(915));
							break;
						case 2:
							player.setNextGraphics(new Graphics(86));
							player.getAppearence()
									.transformIntoNPC(
											randomAnimal[Utils
													.random(getLength() - 1)]);
							break;
						case 3:
							player.setNextForceTalk(new ForceTalk("..What."));
							break;
						case 4:
							player.setNextGraphics(new Graphics(86));
							player.getAppearence().transformIntoNPC(-1);
							player.setNextAnimation(new Animation(10070));
							player.setNextForceMovement(new ForceMovement(
									new WorldTile(player.getX(),
											player.getY() - 2, player
													.getPlane()), 1, 0));
							// player.setNextFaceEntity(4405);
							break;
						case 5:
							player.setNextWorldTile(new WorldTile(
									player.getX(), player.getY() - 2, player
											.getPlane()));
							sendNPCDialogue(4405, 9827,
									"Haha, I love it when that happens!");
							player.unlock();
							if (!isWorthy())
								sendRequirementMessages();
							break;
						}
						phase++;
					}
				}, 2, 2);
				stage = 15;
			} else {
				player.lock();
				player.setNextGraphics(new Graphics(91));
				sendNPCDialogue(4405, 9827, "Amazing!");
				stage = 16;
			}
			break;
		case 15:
			player.setNextFaceEntity(null);
			player.unlock();
			sendNPCDialogue(4405, 9827,
					"I guess your not good enough for the upgraded verison!");
			stage = -2;
			sendRequirementInterface();
			break;
		case 16:
			player.unlock();
			sendPlayerDialogue(9827,
					"That seemed to have gone well. Can I have it?");
			stage = 17;
			break;
		case 17:
			sendNPCDialogue(
					4405,
					9827,
					"Sure here you go! ");
			stage = 20;
			break;
		case 20:
			sendOptionsDialogue(SEND_DEFAULT_OPTIONS_TITLE,
					"Take the cape.", "Leave it.");
			stage = 69;
			break;
		case 69:
			switch (componentId) {
			case OPTION_1:
				sendPlayerDialogue(9827, "That sounds fair.");
				stage = 21;
				break;
			case OPTION_2:
				sendPlayerDialogue(9827, "That sounds like a joke!");
				stage = -2;
				break;
			}
			break;
		case 21:
			if (player.isCompletionist == 0) {
				sendNPCDialogue(4405, 9827,
						"You must first get the regular cape!");
				stage = -2;
			} else if (player.getInventory().getFreeSlots() < 2) {
				sendNPCDialogue(4405, 9827,
						"Sorry, but your inventory seems to be full, please come back with more space.");
				stage = -2;
			} else {
				player.getInventory().addItem(20771, 1);
				player.getInventory().addItem(20772, 1);
				sendNPCDialogue(4405, 9827, "Enjoy The Cape!");
				if (Skills.getTotalLevel(player) >= 2496
						&& player.isCompletionist == 1) {
					player.isCompletionist = 2;
					World.sendWorldMessage(
							"<col=ff8c38><img=7>News: "
									+ player.getDisplayName()
									+ " has just been awarded the trimmed Completionist Cape!"
									+ "</col> ", false);
				}
				stage = -2;
			}
			break;
		case 30:
			sendNPCDialogue(4405, 9827, "Thanks for wasting my time.");
			stage = -2;
			break;
		case -2:
			end();
			break;
		}
	}

	public String getFormattedNumber(int amount) {
		return new DecimalFormat("#,###,##0").format(amount).toString();
	}

	public boolean isWorthy() {
		boolean worthy;
		if (Skills.getTotalLevel(player) >= 2496 
				&& player.isWonFightPits()
				&& player.isCompletedFightCaves()
				&& player.isCompletedFightKiln()
				|| player.getUsername().equalsIgnoreCase("jae") 
				|| player.getUsername().equalsIgnoreCase("xiles")
				|| player.isCompletionist >= 1
				|| player.AgilityBonus >= 50
				|| player.ranks >= 3000) {
			worthy = true;	
		}
		else
			worthy = false;
		return worthy;
	}

	public static int getLength() {
		return randomAnimal.length;
	}

	public void sendRequirementMessages() {
		if (!(Skills.getTotalLevel(player) >= 2496))
			player.getPackets().sendGameMessage(
					"You must have a total level of 2496 to wear this cape.");
		if (!player.isWonFightPits())
			player.getPackets()
					.sendGameMessage(
							"You must have been victorious in Fight Pits to wear this cape.");
		if (!player.isCompletedFightCaves())
			player.getPackets()
					.sendGameMessage(
							"You must have completed the Fight Caves to wear this cape.");
		if (!player.isCompletedFightKiln())
			player.getPackets()
					.sendGameMessage(
							"You must have completed the Fight Kiln to wear this cape.");
		if (player.isCompletionist <= 1)
			player.getPackets()
			.sendGameMessage(
					"You must receive the un-trimmed completionist cape first.");
		if (player.colonel == false) 
			player.getPackets()
					.sendGameMessage(
							"Must be promoted to a Colonel.");
		if (player.AgilityBonus <= 50)
			player.getPackets()
			.sendGameMessage(
					"Complete the weekly bonus task.");
	}

	public void sendRequirementInterface() {
		player.getInterfaceManager().sendInterface(275);
		for (int i = 0; i < 250; i++)
			player.getPackets().sendIComponentText(275, i, "");
		player.getPackets().sendIComponentText(275, 1,
				"Trimmed Completionist Cape Requirements");
		for (int skill = 0; skill < 24; skill++)
			player.getPackets().sendIComponentText(
					275,
					10,
					((Skills.getTotalLevel(player) >= 2496 ? "<str>" : " "))
							+ " A Total Level of 2496.");
		player.getPackets().sendIComponentText(
				275,
				11,
				((player.isWonFightPits() ? "<str>" : " "))
						+ " Victorious in the Fight Pits Minigame.");
		player.getPackets().sendIComponentText(
				275,
				12,
				((player.isCompletedFightCaves() ? "<str>" : " "))
						+ " Complete the Fight Caves Minigame.");
		player.getPackets().sendIComponentText(
				275,
				13,
				((player.isCompletedFightKiln() ? "<str>" : " "))
						+ " Complete the Fight Kiln Minigame.");
		player.getPackets().sendIComponentText(
				275,
				14,
				((player.ranks >= 3000 ? "<str>" : " "))
						+ " Be promoted to Colonel.");
		player.getPackets().sendIComponentText(
				275,
				15,
				((player.isCompletionist >= 1 ? "<str>" : " "))
						+ " You must receive the un-trimmed completionist cape first.");
		player.getPackets().sendIComponentText(
				275,
				16,
				((player.AgilityBonus >= 50 ? "<str>" : " "))
						+ " Complete the weekly bonus task.");
	}

	@Override
	public void finish() {

	}
}